<?php
class TraketquaController extends AppController {
	public $uses = array('Hoso','Tthc','ThanhPhan','ThanhPho','ThanhPhanTthc','Status','User','LichsuHoso','Attachment','PhieuTrinh','Ketquahanhchinh');
	
	public function beforeFilter() {
		$this->layout = 'quanly';
	}
	
	public function index($status = null) {
		$st = array(
			$this->Status->HS_CHO_TRA_KQ,
			$this->Status->HS_DA_TRA_KQ,
			$this->Status->HS_TRE_HEN,
			$this->Status->HS_KHONG_DAT_CHO_TRA_KQ,
			$this->Status->HS_KHONG_DEN_NHAN_KQ);
			
		if (!empty($status) && in_array($status, $st)) {
			$st = array($status);
		}
		
		$listStatus = array(
			1 => 'All',
			$this->Status->HS_CHO_TRA_KQ => 'Hồ sơ chờ trả kết quả',
			$this->Status->HS_DA_TRA_KQ => 'Hồ sơ đã trả kết quả',
			$this->Status->HS_TRE_HEN => 'Hồ sơ chờ trễ hẹn',
			$this->Status->HS_KHONG_DAT_CHO_TRA_KQ => 'Hồ sơ không đạt, chờ trả kết quả',
			$this->Status->HS_KHONG_DEN_NHAN_KQ => 'Hồ sơ không đến nhận kết quả');
			
		$this->set('listStatus', $listStatus);
		$this->set('status', $status);
		
		$listHosos = $this->Hoso->find("all", array(
			'conditions' => array('Hoso.trang_thai' => $st) 
		));
		
		$this->set('listHosos', $listHosos);
	}
	
	public function traketqua ($id = null) {
		$inStatus = array(
			$this->Status->HS_CHO_TRA_KQ,
			$this->Status->HS_DA_TRA_KQ,
			$this->Status->HS_TRE_HEN,
			$this->Status->HS_KHONG_DAT_CHO_TRA_KQ,
			$this->Status->HS_KHONG_DEN_NHAN_KQ);
			
		$hoso = $this->Hoso->findById($id);
		
		$thanhPhos = $this->ThanhPho->find("list", array('fields'=>array('code','ten')));
		$this->set('thanhPhos', $thanhPhos);
		
		$this->User->recursive = -1;
		
		$users = $this->User->find("list", array("conditions" => array("User.username !=" => 'admin'), 'fields'=>array('id','username')));
		$his = $this->LichsuHoso->find("all", array("conditions" => array("LichsuHoso.hoso_id" => $id)));
		
		$this->set("enable", 0);
		
		if(in_array($hoso['Hoso']['trang_thai'], $inStatus)) {
			$this->set("enable", 1);
		}
		
		// calculate ngay qua han
		$date1 = new DateTime();
		$date2 = new DateTime($hoso['Hoso']['ngay_hen_tra']);
		($this->compareDate($date1, $date2) < 0) && $this->set("qua_han", $this->compareDate($date1, $date2));
			
		// debug($users);
		$this->set("his", $his);
		$this->set("hoso", $hoso);
		$this->set("users", $users);
		$this->set("selectedHoso", $id);
	}
	
	public function tra ($id = null) {
		if (!empty($this->request->data)) {
			$this->Hoso->id = $this->request->data['Hoso']['id'];
			$this->Hoso->saveField('trang_thai', $this->Status->HS_DA_TRA_KQ );
			$this->Hoso->save();
			
			$this->redirect("index");
		}
	}
	public function trehen ($id = null) {
		if (!empty($this->request->data)) {
			$this->Hoso->id = $this->request->data['Hoso']['id'];
			$this->Hoso->saveField('trang_thai', $this->Status->HS_TRE_HEN );
			$this->Hoso->save();
			
			$this->redirect("index");
		}
	}
	public function khongdenlay ($id = null) {
		if (!empty($this->request->data)) {
			$this->Hoso->id = $this->request->data['Hoso']['id'];
			$this->Hoso->saveField('trang_thai', $this->Status->HS_KHONG_DEN_NHAN_KQ);
			$this->Hoso->save();
			
			$this->redirect("index");
		}
	}
	
	/**
	 * Xem Hoso truoc de xuly
	 */
	public function xemHoso ($id = null) {
		if (!empty($this->request->data)) {
			$date = !empty($this->request->data['Hoso']['ngay_tra'])?$this->convertStrDate($this->request->data['Hoso']['ngay_tra']): NULL;
			
			$this->Hoso->id = $this->request->data['Hoso']['id'];
			$this->Hoso->saveField('ghi_chu', $this->request->data['Hoso']['ghi_chu']);
			!empty($date)?$this->Hoso->saveField('ngay_hen_tra', $date):'';
			$this->Hoso->save();
			
			$this->redirect(array("action" => "xuly",$this->Hoso->id));
		}
		
		$hoso = $this->Hoso->findById($id);
		$thanhPhos = $this->ThanhPho->find("list", array('fields'=>array('code','ten')));
		$this->set('thanhPhos', $thanhPhos);
		
		if (!empty($hoso)){
			$thanhPhans = $this->ThanhPhan->getListThanhPhanGiayToByTthcId($hoso['Tthc']['id']);
			$this->set("thanhPhans", $thanhPhans);
			
			$attachments = $this->Attachment->find("all", array('conditions' => array("hoso_id" => $id)));
			$this->set("attachments", $attachments);
		} else {
			$this->log("Invalid document Id:"+$id);
		}
		
		$hoso['Hoso']['ngay_tra'] = empty($hoso['Hoso']['ngay_hen_tra'])?"":CakeTime::format('d/m/Y',$hoso['Hoso']['ngay_hen_tra']);
		//export value to view
		$this->set("hoso", $hoso);
		$this->set("selectedHoso", $id);
	}

	public function ketquahanhchinh ($id = null) {
		$hoso = $this->Hoso->findById($id);
		
		$ketquas = $this->Ketquahanhchinh->find("all", array("conditions" => array("Ketquahanhchinh.hoso_id" => $id)));
		
		
		// calculate ngay qua han
		$date1 = new DateTime();
		$date2 = new DateTime($hoso['Hoso']['ngay_hen_tra']);
		($this->compareDate($date1, $date2) < 0) && $this->set("qua_han", $this->compareDate($date1, $date2));
		
		$this->set("hoso", $hoso);
		$this->set("ketquas", $ketquas);
		$this->set("selectedHoso", $id);
	}
}
